package org.example.controller;

import org.example.entity.Menu;
import org.example.entity.Order;
import org.example.entity.OrderVO;
import org.example.entity.User;
import org.example.feign.OrderFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/order")
public class OrderHandler {
    @Autowired
    private OrderFeign orderFeign;

    @GetMapping("")
    public String person_order() {
        return "person_order";
    }

    @GetMapping("/save/{mid}")
    public String save(@PathVariable("mid") int mid, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Order order = new Order();
        order.setUser(user);
        Menu menu = new Menu();
        menu.setId(mid);
        order.setMenu(menu);
        order.setDate(new Date());
        orderFeign.save(order);
        return "index";
    }

    @GetMapping("/findAllByState")
    @ResponseBody
    public OrderVO findAllByState(HttpSession session, @RequestParam("page") int page, @RequestParam("limit") int limit) {
        int index = (page - 1) * limit;
        return orderFeign.findAllByState(index, limit);
    }

    @GetMapping("/findAllByUid")
    @ResponseBody
    public OrderVO findAllByUid(HttpSession session, @RequestParam("page") int page, @RequestParam("limit") int limit, @RequestParam("uid") long uid) {
        int index = (page - 1) * limit;
        return orderFeign.findAllByUid(index, limit, uid);
    }

    //前端的修改是通过超链接发送的，应用@Getmaping注解
    @GetMapping("/updateState/{id}")
    public String updateState(@PathVariable("id") long id) {
        orderFeign.updateState(id);
        return "redirect:/menu/redirect/order_handler";
    }


    @GetMapping("/pay")
    @ResponseBody
    public String pay(HttpSession session, @RequestParam("tradeNo") String tradeNo, @RequestParam("subject") String subject
            , @RequestParam("totalAmount") String totalAmount) {
         return  orderFeign.pay(tradeNo, subject, totalAmount);
    }

    @GetMapping("/ali")
    @ResponseBody
    public String ali(HttpSession session, @RequestParam("tradeNo") String tradeNo, @RequestParam("subject") String subject
            , @RequestParam("totalAmount") String totalAmount) {
       return  orderFeign.alipay(tradeNo, subject, totalAmount);
        //return "<form name=\"punchout_form\" method=\"post\" action=\"https://openapi-sandbox.dl.alipaydev.com/gateway.do?charset=UTF8&method=alipay.trade.page.pay&sign=fpxaKyanNchDo3tQEfDgeB9tB0KNp6HIy%2FMtXPXQJ0ht9g5U12qezaEgt9ko8jmceUuqmAozm60wcGl3WXQWVUB5%2F7t1Y4Ii42lN6AuSlN2LC0mBwRmgJkomW6Jfkjq7%2B4ZkLwOEzBaL2le1uY%2F%2B10sxdwHuAuSyLbvd1u%2FyxEK0CpRY56C2%2BVR%2BsxMDjuliy82meE72j%2B8FNvvF4pwbY27bV7Edp9zZBd%2BecqU9YlHgFhDE4bZX7VX2%2BF7C7aTOUqD9Kq8pjMC%2FEs6I2Cr7RzqJPJd7xPhBkKvcBBFcLvFtmfmCXwlZ5p6hy5NEViW0DFfNxXsUbYD62r7e08tySA%3D%3D&app_id=9021000149689800&sign_type=RSA2&timestamp=2025-06-26+10%3A22%3A41&alipay_sdk=alipay-sdk-java-4.35.171.ALL&format=json\"> <input type=\"hidden\" name=\"biz_content\" value=\"{&quot;out_trade_no&quot;:&quot;20150320010101001&quot;,&quot;total_amount&quot;:&quot;88.88&quot;,&quot;subject&quot;:&quot;Iphone6 16G&quot;,&quot;product_code&quot;:&quot;FAST_INSTANT_TRADE_PAY&quot;}\"> <input type=\"submit\" value=\"立即支付\" style=\"display:none\" > </form> <script>document.forms[0].submit();</script>";
    }
}
